<template>
  <div>
    <div class="peosonResultListBox main w" style="background-color:#fff">
      <p class="peosonResultTitle">日常评价情况</p>
      <div class="switchResultBox">
        <div class="selectDepBox">
          <span class="selectTitle">选择是否满意 :</span>
          <span>
            <el-select
              v-model="isSatisfied"
              placeholder="请选择"
              @change="handleSatisfyChange"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </span>
        </div>
        <div class="selectDepBox">
          <span class="selectTitle">选择部门 :</span>
          <span>
            <el-select
              v-model="departmentName"
              clearable
              value-key="depid"
              placeholder="请选择部门"
              @change="handleDeparentChange"
              >
              <el-option
                v-for="item in vestinList"
                :key="item.depid"
                :label="item.name"
                :value="item.depid"
              ></el-option>
            </el-select>
          </span>
        </div>
      </div>
      <!-- 列表 -->
      <el-table
        :data="personResultList"
        border
        @row-click="goPersonResult"
        class="resultTable"
        :header-cell-style="elementStyleObj"
        >
        <el-table-column type="index" label="序号" width="50" header-align="center" align="center"></el-table-column>
        <el-table-column prop="time" label="时间" width="180" header-align="center" align="center"></el-table-column>
        <el-table-column prop="name" label="姓名" width="170" header-align="center" align="center"></el-table-column>
        <el-table-column prop="departmentName" label="所属部门" header-align="center" align="center"></el-table-column>
        <el-table-column
          prop="satisfiedCount"
          label="满意(次)"
          width="100"
          header-align="center"
          align="center"
          empty-text="暂无评价内容"
        ></el-table-column>
        <el-table-column
          prop="unSatisfiedCount"
          label="不满意(次)"
          width="120"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="isRead"
          label="筛选"
          width="100"
          :filters="[{ text: '未读', value: false }, { text: '已读', value: true }]"
          :filter-method="filterTag"
          filter-placement="bottom-end"
          header-align="center"
          align="center"
        >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.isRead ===false ? 'primary' : 'success'"
              disable-transitions
            >{{scope.row.isRead == false? "未读":"已读"}}</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="page-box" style="margin: 5px 0px 80px 200px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10,20,50,100,300]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageLen"
          v-if="pageLen != 0"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<style scoped>
/* alert提示框样式 */

.resultTable {
  font-family: "Microsoft" !important;
  width: 88%;
  margin: 50px 6% 30px;
  -webkit-box-shadow: 0px 6px 14px #e8e8e8;
  box-shadow: 0px 6px 14px #e8e8e8;
  cursor: pointer;
}
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
<script>
import bus from "../../components/common";
import { ISPINGYI } from "../../components/common";

export default {
  data() {
    return {
      personResultList: [], //日常评价数据
      pageLen: 0, //总条数
      currentPage: 1, //当前默认加载的页码
      pageSize: 50, //页容量
      //element 表头样式
      elementStyleObj: {
        background: "rgb(245, 249, 255) ",
        fontSize: "18px",
        color: "#434343"
      },
      //是否满意
      options: [
        
        {
          value: "",
          label: "查看全部"
        },
        {
          value: true,
          label: "不满意"
        },
        {
          value: false,
          label: "满意"
        }
      ],
      vestinList: [], //查询被部门列表
      isSatisfied: "", //是否满意
      departmentId: "", //部门ID
      departmentData: {}, //选中的值
      departmentName:"",//部门name
      
    };
  },
  created() {
    this.currentPage = this.getContextData("currentPage") || 1;
    this.pageSize = this.getContextData("pageSize") || 50;
    this.isSatisfied = this.getContextData("isSatisfied") || '';
    this.departmentId = this.getContextData("departmentId") ||'';
    this.departmentName = this.getContextData("departmentName") || '' ;
    this.getpersonResultList(); //获取评价结果列表
    this.getVestinList(); //被评价部门列表
  },
  methods: {
    //选择部门
    handleDeparentChange(depId) {
      this.departmentId = depId;
      this.departmentData = this.vestinList.find(item => {
        return item.depid === depId; //筛选出匹配数据
      });
      // console.log("当前选中的部门数据", this.departmentData); 
      // console.log("当前选中的部门name", this.departmentData.name); 
      this.setContextData("departmentName", this.departmentData.name);
      this.setContextData("departmentId",this.departmentId)
      let currentPage = 1 //页码等于1
      this.handleCurrentChange(currentPage); //调用当前页码
    },
    //给sessionStorage存值
    setContextData: function(key, value) {
      if (typeof value == "string") {
        sessionStorage.setItem(key, value);
      } else {
        sessionStorage.setItem(key, JSON.stringify(value));
      }
    },
    // 从sessionStorage取值
    getContextData: function(key) {
      const str = sessionStorage.getItem(key);
      if (typeof str == "string") {
        try {
          return JSON.parse(str);
        } catch (e) {
          return str;
        }
      }
      return;
    },
    // 前往个人评价详情页
    goPersonResult(val, row) {
      this.$router.push({
        name: "personResult",
        params: {
          eid: val.employeeId,
          departmentId: val.departmentId
        }
      });
      // window.open(routeData, '_blank');
    },
    //已读未读标签
    filterTag(value, row) {
      // console.log("filter","val",value ,"row",row);
      return row.isRead === value;
    },
   
    // 是否满意选项
    handleSatisfyChange(value) {
      this.isSatisfied = value;
      this.setContextData("isSatisfied",this.isSatisfied)
      let currentPage = 1 //页码等于1
      this.handleCurrentChange(currentPage); //调用当前页码
    },
    //element分页条相关
    //当更改了页容量
    handleSizeChange(size) {
      this.pageSize = size;
      this.setContextData("pageSize",this.pageSize)
      let currentPage = 1 //页码等于1
      this.handleCurrentChange(currentPage); //调用当前页码
    },
    //当更改了当前的页码
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.setContextData("currentPage",this.currentPage);
      this.getpersonResultList();
    },
    //获取评价数据
    getpersonResultList() {
      const url = `api/message/list?pageSize=${this.pageSize}&pageNumber=${
        this.currentPage
      }&departmentId=${this.departmentId}&isSatisfied=${this.isSatisfied}`;
      this.axios.post(url).then(res => {
        // console.log("data", res.data);
        this.personResultList = res.data;
      });
      this.getPageLen(); //日常评价总条数
    },
    //获取部门列表
    getVestinList() {
      this.axios
        .get("api/employee/statistics/getDepartments")
        .then(response => {
          let depList = response.data; 
          // console.log("消息中心部门列表", depList);
          //模拟全部部门选项
          let allDep = {
            name:"查看所有部门",
            depid:"",
          }
          depList.unshift(allDep);
          this.vestinList = depList
          // console.log("newVestinList",this.vestinList);
        });
    },
    //获取评论总条数
    getPageLen() {
      const url = `api/message/countAll?departmentId=${
        this.departmentId
      }&isSatisfied=${this.isSatisfied}`;
      this.axios.post(url).then(res => {
        // console.log("总条数", res.data);
        this.pageLen = res.data;
      });
    }
  }
};
</script>

